import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:my_app/router/index.dart';

const TextStyle btnts = TextStyle(fontSize: 16);

class NavigationPage extends StatelessWidget {
  const NavigationPage({
    super.key,
    required this.child
  });

  final Widget child;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: child,
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _calculateSelectedIndex(context),
        items: const [
          BottomNavigationBarItem(
            icon: Icon(Icons.widgets),
            label: "Widget Demo",
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.view_module),
            label: "Plugin Demo",
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.api),
            label: "Api Demo",
          )
        ],
        onTap: (idx) => _onItemTapped(idx, context),
      ),
    );
  }

  /// 由于增强枚举中的path值是final修饰的，所以不能使用switch..case
  /// 
  /// 后面有好办法的时候再处理这个if..else
  /// 
  /// 参考：https://github.com/dart-lang/sdk/issues/50166
  int _calculateSelectedIndex(BuildContext context) {
    final location = GoRouterState.of(context).location;
    int current = 0;
    if (location == NavigationRouters.widgetDemo.path) {
      current = 0;
    }
    else if (location == NavigationRouters.pluginDemo.path) {
      current = 1;
    }
    else if (location == NavigationRouters.apiDemo.path) {
      current = 2;
    }
    return current;
  }

  void _onItemTapped(int index, BuildContext context) {
    switch (index) {
      case 0:
        GoRouter.of(context).go(NavigationRouters.widgetDemo.path);
        break;
      case 1:
        GoRouter.of(context).go(NavigationRouters.pluginDemo.path);
        break;
      case 2:
        GoRouter.of(context).go(NavigationRouters.apiDemo.path);
        break;
    }
  }
}
